#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	int n,i=0,j; int gchinese=0, gmath=0, genglish=0, gprogram=0;
	struct student { int id; int chinese; int math; int english; int program; int general; };
	struct student name[100];
	printf("输入人数：");
	scanf("%d", &n);
	printf("不排名的成绩\n");
	srand(time(0));
	while (i < n)
	{
		name[i].id = i + 1;
		name[i].chinese = (rand() % 25) + 70;
		name[i].math= (rand() % 60) + 30;
		if((name[i].chinese)>80)
		name[i].english= (rand() % 25) + 70;
		else
			name[i].english = (rand() % 25) + 60;
		if(name[i].math>60)
		name[i].program= (rand() % 25) + 60;
		else
			name[i].program = (rand() % 25) + 40;
		i++;
    }
	
//每门总成绩
	for (i = 0; i < n; i++)
		gchinese += name[i].chinese;
	for (i = 0; i < n; i++)
		gmath+= name[i].math;
	for (i = 0; i < n; i++)
		genglish += name[i].english;
	for (i = 0; i < n; i++)
		gprogram += name[i].program;
//每门平均成绩
	int achinese, amath, aenglish, aprogram;
	achinese=gchinese / n; 
	amath=gmath / n;
	aenglish= genglish / n;
	aprogram= gprogram / n;
//每个学生总成绩
	int* gstudent;
	gstudent = (int*)malloc(n * sizeof(int));
	for (i = 0; i < n; i++)
		gstudent[i] = name[i].chinese + name[i].math + name[i].english + name[i].program;
//每个学生平均成绩
	int* astudent;
	astudent= (int*)malloc(n * sizeof(int));
	for (i = 0; i < n; i++)
		astudent[i] = gstudent[i] / 4;
//每门最大成绩
	int maxc,maxm, maxe, maxp;
	maxc = name[0].chinese;
	for (i = 1; i < n; i++)
		maxc = maxc < (name[i].chinese) ? (name[i].chinese) : maxc;
	maxm = name[0].math;
	for (i = 1; i < n; i++)
		maxm= maxm < (name[i].math) ? (name[i].math) : maxm;
	maxe = name[0].english;
	for (i = 1; i < n; i++)
		maxe = maxe < (name[i].english) ? (name[i].english) : maxe;
	maxp = name[0].program;
	for (i = 1; i < n; i++)
		maxp = maxp < (name[i].program) ? (name[i].program) : maxp;

	printf("id chinese math english program general average\n");
	for (i = 0; i < n; i++)
	{
		printf("%-3d ", i + 1);
		printf("%-8d%-8d%-8d%-8d%-8d%-8d\n", name[i].chinese, name[i].math, name[i].english, name[i].program, gstudent[i], astudent[i]);
	}
	
	printf("总分%-8d%-8d%-8d%-8d\n", gchinese , gmath, genglish , gprogram);
	printf("平均%-8d%-8d%-8d%-8d%\n", achinese, amath, aenglish, aprogram);
	printf("max %-8d%-8d%-8d%-8d", maxc, maxm, maxe, maxp);
	i = 0;
	while (i < n)
	{
		name[i].general = gstudent[i]; i++;
	}
//排名
	struct student temp;
	for(i=0;i<n-1;i++)
		for(j=i+1;j<n;j++)
			if ((name[i].general) <( name[j].general))
			{
				temp = name[i];
				name[i] = name[j];
				name[j] = temp;
            }
	printf("\n\n\n排名后的成绩\n");
	printf("排名 id chinese math english program general \n");

	for (i = 0; i < n; i++)
	{
		printf("%2d   ", i + 1);
		printf("%-2d %-8d%-8d%-8d%-8d%-8d\n", name[i].id, name[i].chinese,name[i].math, name[i].english, name[i].program, name[i].general);
	}
	/*printf("总分    %-8d%-8d%-8d%-8d\n", gchinese, gmath, genglish, gprogram);*/
	printf("平均    %-8d%-8d%-8d%-8d%-8d\n", achinese, amath, aenglish, aprogram, achinese+amath+aenglish+aprogram);
	printf("max     %-8d%-8d%-8d%-8d", maxc, maxm, maxe, maxp);
}